package com.vcsoft.emailresponder.parser;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.vcsoft.emailresponder.config.bean.Database;
import com.vcsoft.emailresponder.util.Utility;

public class DatabaseParser extends AbstractParser<Database>{

	/**
	 * Object of database to set all the properties after parsing done from config.xml
	 */
	Database databaseConfig;
	
	/**
	 * Public constructor
	 */
	public DatabaseParser() {
	}

	@Override
	public List<Database> parse(NodeList nodeList) {
		if(nodeList == null){
			System.out.println("nodeList is null");
			return null;
		}
		
		List<Database> databaseConfig = new ArrayList<Database>();

		for (int idx = 0; idx < nodeList.getLength(); idx++) {
			Database database = new Database();

			Element element = (Element) nodeList.item(idx);
			//String id = element.getAttribute("ID");

			//Host Name
			NodeList hostNameNodes = element.getElementsByTagName("hostName");
			Element eleHostName = (Element) hostNameNodes.item(0);
			database.setHostName(Utility.getCharacterDataFromElement(eleHostName));
			
			// Database Name
			NodeList dbNameNodes = element.getElementsByTagName("dbName");
			Element eleDbName = (Element) dbNameNodes.item(0);
			database.setDatabaseName(Utility.getCharacterDataFromElement(eleDbName));
			
			// User Name
			NodeList userNameNodes = element.getElementsByTagName("userName");
			Element eleUserName = (Element) userNameNodes.item(0);
			database.setUserName(Utility.getCharacterDataFromElement(eleUserName));
			
			// password
			NodeList pwdNameNodes = element.getElementsByTagName("password");
			Element elePassword = (Element) pwdNameNodes.item(0);
			database.setPassword(Utility.getCharacterDataFromElement(elePassword));
			
			databaseConfig.add(database);
		}
		return databaseConfig;
	}

	/*private void printParsedData(){
		NodeList nodes = super.getNodeList("database");
		DatabaseParser spe = new DatabaseParser();
		List<Database> dbc = spe.parse(nodes);
		for (Iterator iterator = dbc.iterator(); iterator.hasNext();) {
			Database database = (Database) iterator.next();
			System.out.println(database.getHostName());
			System.out.println(database.getDatabaseName());
			System.out.println(database.getUserName());
			System.out.println(database.getPassword());
		}
	}*/
	 
	/*public static void main(String[] args) {
		DatabaseParser spe = new DatabaseParser();
		spe.printParsedData();
	}*/
}
